//
//  SLMapController.m
//  ContactsCoreData
//
//  Created by dllo on 15/11/21.
//  Copyright © 2015年 张树林. All rights reserved.
//

#import "SLMapController.h"



@interface SLMapController () <BMKMapViewDelegate>

@property (strong, nonatomic) BMKMapView *mapView;

@end

@implementation SLMapController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
     self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    self.view = self.mapView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    
    // 设置标注
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    
    annotation.coordinate = self.location;
    annotation.title = self.address;
    [_mapView addAnnotation:annotation];
    
    // 设定地图中心点
    [_mapView setCenterCoordinate:self.location animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
}


- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        return newAnnotationView;
    }
    return nil;
}

@end
